package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @author Matthias
 *
 */
public class AccWithdrawal extends JPanel{
	
	private JLabel lbl_Header;
	private JLabel lbl_Intro;
	private JLabel lbl_CurrentBalance;
	private JLabel lblForCustomAmount;
	private JTextField textField_CurrentBalance;
	private JButton btn_Back;
	private JButton btn_Withdraw;
	private JButton btn_CustomAmount;
	private JRadioButton rdbtn_20;
	private JRadioButton rdbtn_40;
	private JRadioButton rdbtn_60;
	private JRadioButton rdbtn_80;
	private JRadioButton rdbtn_100;
	private JRadioButton rdbtn_200;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	
	String input;
	
	public AccWithdrawal() {
		
		setPreferredSize(new Dimension(550, 400));
		setLayout(null);
		
		lbl_Header = new JLabel("Account Withdrawal");
		lbl_Header.setFont(new Font("SansSerif", Font.BOLD, 26));
		lbl_Header.setBounds(168, 38, 252, 34);
		add(lbl_Header);
		
		lbl_Intro = new JLabel("Chose an ammount:");
		lbl_Intro.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Intro.setBounds(51, 97, 155, 21);
		add(lbl_Intro);
		
		btn_Back = new JButton("Back");
		btn_Back.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Back.setBounds(51, 300, 100, 40);
		add(btn_Back);
		
		btn_Withdraw = new JButton("Withdraw");
		btn_Withdraw.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Withdraw.setBounds(357, 300, 100, 40);
		add(btn_Withdraw);
		
		rdbtn_20 = new JRadioButton("20");
		rdbtn_20.setActionCommand("20");
		rdbtn_20.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_20);
		rdbtn_20.setBounds(51, 122, 66, 29);
		add(rdbtn_20);
		
		rdbtn_40 = new JRadioButton("40");
		rdbtn_40.setActionCommand("40");
		rdbtn_40.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_40);
		rdbtn_40.setBounds(168, 122, 70, 29);
		add(rdbtn_40);
		
		rdbtn_60 = new JRadioButton("60");
		rdbtn_60.setActionCommand("60");
		rdbtn_60.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_60);
		rdbtn_60.setBounds(51, 158, 66, 29);
		add(rdbtn_60);
		
		rdbtn_80 = new JRadioButton("80");
		rdbtn_80.setActionCommand("80");
		rdbtn_80.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_80);
		rdbtn_80.setBounds(168, 164, 70, 23);
		add(rdbtn_80);
		
		rdbtn_100 = new JRadioButton("100");
		rdbtn_100.setActionCommand("100");
		rdbtn_100.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_100);
		rdbtn_100.setBounds(51, 198, 66, 29);
		add(rdbtn_100);
		
		rdbtn_200 = new JRadioButton("200");
		rdbtn_200.setActionCommand("200");
		rdbtn_200.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_200);
		rdbtn_200.setBounds(168, 198, 70, 29);
		add(rdbtn_200);
		
		btn_CustomAmount = new JButton("Custom Amount");
		btn_CustomAmount.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_CustomAmount.setBounds(312, 235, 150, 40);
		add(btn_CustomAmount);
		
		textField_CurrentBalance = new JTextField();
		textField_CurrentBalance.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		textField_CurrentBalance.setHorizontalAlignment(SwingConstants.CENTER);
		textField_CurrentBalance.setEditable(false);
		textField_CurrentBalance.setBounds(297, 122, 160, 30);
		add(textField_CurrentBalance);
		textField_CurrentBalance.setColumns(10);
		
		lbl_CurrentBalance = new JLabel("Current Balance:");
		lbl_CurrentBalance.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_CurrentBalance.setBounds(312, 97, 130, 21);
		add(lbl_CurrentBalance);
		
		lblForCustomAmount = new JLabel("For Custom Amount press:");
		lblForCustomAmount.setFont(new Font("SansSerif", Font.BOLD, 16));
		lblForCustomAmount.setBounds(250, 202, 207, 21);
		add(lblForCustomAmount);
	}
	
	public void addActionListener (ActionListener e){
		btn_Back.addActionListener(e);
		btn_Withdraw.addActionListener(e);
		btn_CustomAmount.addActionListener(e);
	}
	
	public void setRdbtnsFalse() {
		rdbtn_20.setEnabled(false);
		rdbtn_40.setEnabled(false);
		rdbtn_60.setEnabled(false);
		rdbtn_80.setEnabled(false);
		rdbtn_100.setEnabled(false);
		rdbtn_200.setEnabled(false);
		buttonGroup.clearSelection();
	}
	
	public void setRdbtnsTrue() {
		rdbtn_20.setEnabled(true);
		rdbtn_40.setEnabled(true);
		rdbtn_60.setEnabled(true);
		rdbtn_80.setEnabled(true);
		rdbtn_100.setEnabled(true);
		rdbtn_200.setEnabled(true);
		buttonGroup.clearSelection();
	}
	
	public JButton getBack(){
		input = "";
		setRdbtnsTrue();
		return btn_Back;
	}
	
	public JButton getBtnWithdrawal(){
		return btn_Withdraw;
	}
	
	public JButton getBtnCustomAmount(){
		return btn_CustomAmount;
	}
	
	public void showField(double totalBalance){
		textField_CurrentBalance.setText("" + totalBalance);
	}
	
	public void setInput() {
		input = null;
	}
	
	public void showDialog() {
		input = JOptionPane.showInputDialog(null, "", "Enter custom amount", JOptionPane.OK_CANCEL_OPTION);
		if(input == null){
			// if button cancel is clicked it goes back to the screen
			setRdbtnsTrue();
			System.out.println("showDialog?" + input);
		}
		else {
			// enables any radiobutton to be clicked, in order to avoid misunderstandings
			setRdbtnsFalse();
			JOptionPane.showMessageDialog(null, "Withdraw input: " + input + "\nPlease confirm the withdrawal"
					+ " by clicking Withdraw", 
					"Custom Withdrawal succeeded", JOptionPane.INFORMATION_MESSAGE);	
		}
	}
	
	public double getWithdrawInputOfDialog() {
		System.out.println("getInputOfDialog?" + input);
		if (input == null ){
			return 0;
		}
		else {
			System.out.println("custom input???" + input);
			return Double.parseDouble(input);
		}
	}
			
	public double getWithdrawRdbtnAmount() {
		setInput();
		ButtonModel model = buttonGroup.getSelection();
		if (model == null) { // no radiobutton selected
			return 0;
		} else {
			buttonGroup.clearSelection();
			return Integer.parseInt(model.getActionCommand());
		}
	}
}
